(function($){
    var activity ={
        init:function(){
            this.datePicker();
            this.activitySave();
            this.pictureSelect();
            this.reset();
        },
        //活动设置时间选择器
        datePicker:function(){
            $('#reg_start').datetimepicker({
                format: 'yyyy-mm-dd',
                startView:2,
                minView:2
            }).on('changeDate',function(ev){
                var s = $('#reg_start').val();
                $('#reg_end').datetimepicker({
                    format: 'yyyy-mm-dd',
                    startView:2,
                    minView:2,
                    startDate:s
                });
            });
            $('#vote_start').datetimepicker({
                format: 'yyyy-mm-dd',
                startView:2,
                minView:2
            }).on('changeDate',function(ev){
                var s = $('#vote_start').val();
                $('#vote_end').datetimepicker({
                    format: 'yyyy-mm-dd',
                    startView:2,
                    minView:2,
                    startDate:s
                });
            });
        },
        //保存信息
        activitySave:function(){
            $('#activitySave').click(function(){
                var error = false;
                var name = $('#name').val();//标题
                if(!name){
                    error=1;
                    $('#name').parent().addClass('has-error');
                }else{
                    $('#name').parent().removeClass('has-error');
                }
                var picture = $('#picture').val();
                if(!picture){
                    error = 1;
                    $('#pic_div').addClass('has-error');
                }else{
                    $('#pic_div').remove('has-error');
                }
                var template = $('#template').val();
                var reg_start = $('#reg_start').val();
                var reg_end = $('#reg_end').val();
                if(!reg_start || !reg_end){
                    error = 1;
                    $('#reg_div').addClass('has-error');
                }else{
                    $('#reg_div').removeClass('has-error');
                }
                var vote_start = $('#vote_start').val();
                var vote_end = $('#vote_end').val();
                if(!vote_start || !vote_end){
                    error = 1;
                    $('#vote_div').addClass('has-error');
                }else{
                    $('#vote_div').removeClass('has-error');
                }
                var rule = $('#rule').val();
                if(!rule){
                    error =1;
                    $('#rule').parent().addClass('has-error');
                }else{
                    $('#rule').parent().removeClass('has-error');
                }
                var prize = $('#prize').val();
                if(!prize){
                    error =1;
                    $('#prize').parent().addClass('has-error');
                }else{
                    $('#prize').parent().removeClass('has-error');
                }
                var share_title = $('#share_title').val();
                if(!share_title){
                    error =1;
                    $('#share_title').parent().addClass('has-error');
                }else{
                    $('#share_title').parent().removeClass('has-error');
                }
                var share_describe = $('#share_describe').val();
                if(!share_describe){
                    error =1;
                    $('#share_describe').parent().addClass('has-error');
                }else{
                    $('#share_describe').parent().removeClass('has-error');
                }
                var pub = $('#public').val();
                var follow =$('#follow').val();//是否需要关注公众号
                var vote_time=0;
                if($("input[name='optionsRadios']:checked").val() ==1){
                    vote_time =$('#vote_time').val();
                    if(!vote_time){
                        error =1;
                        $('#vote_time_div').addClass('has-error');
                    }else{
                        $('#vote_time_div').removeClass('has-error');
                    }
                }
                var pv = $('#pv').val();
                if(error){
                    layer.alert('请先修改被标红的提交项',{title:'保存失败！'});
                    return false;
                }
                //提交
                var data = {
                    aid:$('#aid').val(),
                    name:name,
                    picture:picture,
                    template:template,
                    reg_start:reg_start,
                    reg_end:reg_end,
                    vote_start:vote_start,
                    vote_end:vote_end,
                    rule:rule,
                    prize:prize,
                    share_title:share_title,
                    share_describe:share_describe,
                    pub:pub,
                    follow:follow,
                    vote_time:vote_time,
                    pv:pv,
                    ext:$('#ext').val(),
                };
                $.ajax({
                    url:"http://wams.veryeast.cn/activity/meadin_activity/admin/index.php?a=activityEditHandle",
                    type:"post",
                    data:data,
                    dataType:'json',
                    success:function(data){
                        if(data.status==1){
                            layer.alert("保存成功！");
                            $('#picture').val('uploaded');
                        }else{
                            layer.alert("保存失败！");
                        }
                    }
                });

            });
        },
        pictureSelect:function(){
            $('#pictureSelect').change(function(){
                //判断浏览器是否支持FileReader接口
                if(typeof FileReader == 'undefined'){
                    alyer.alert("您的浏览器不支持！请选择ie9，chrome，火狐浏览器！");
                    return false;
                }
                file = this.files[0];
                if(!/image\/\w+/.test(file.type)){
                    layer.alert('请选择图片类型的文件');
                    return false;
                }
                var reader = new FileReader();
                //将文件以Data URL形式读入页面
                reader.readAsDataURL(file);
                reader.onload=function(e){
                    $('#preview').attr('src',this.result);
                    $('#picture').val(this.result);
                    $('#ext').val(file.name.substr(file.name.indexOf('.')+1));
                };


                //清空选择
                $(this).val('');
            });
        },
        reset:function(){
            $('#reset').click(function(){
                window.location.reload();
            });
        }
    };
    activity.init();
})(jQuery);